<?php


namespace App\Http\Controllers\Api\Student;

use App\Services\Student\StudentServices;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class StudentController extends Controller
{
    /**
     * 获取学生信息
     *
     * @param Request            $request
     * @param StudentServices      $StudentServices
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     */
    public function list(Request $request,StudentServices  $StudentServices)
    {
        $this->validate($request, [
            'id' => 'required|integer',
        ]);
        $data = $StudentServices->getList($request->all());
        if ($data === false) {
            return json_encode([]);
        }
        return json_encode($data,true);
    }

    /**
     * 新增学生信息
     *
     * @param Request            $request
     * @param StudentServices      $StudentServices
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     */
    public function add(Request $request,StudentServices  $StudentServices)
    {

        $this->validate($request, [
            'name' => 'required|string',
            'class_id' => 'required|integer',
        ]);
        $data = $StudentServices->add($request->all());
        if ($data === false) {
            return '操作失败';
        }
        return '操作成功';
    }

    /**
     * 编辑学生信息
     *
     * @param Request            $request
     * @param StudentServices      $StudentServices
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     */
    public function edit(Request $request,StudentServices  $StudentServices)
    {

        $this->validate($request, [
            'id' => 'required|integer',
            'name' => 'required|string',
        ]);
        $data = $StudentServices->edit($request->all());
        if ($data === false) {
            return '操作失败';
        }
        return '操作成功';
    }


    /**
     * 删除学生信息
     *
     * @param Request            $request
     * @param StudentServices      $StudentServices
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     */
    public function del(Request $request,StudentServices  $StudentServices)
    {

        $this->validate($request, [
            'id' => 'required|integer'
        ]);
        $data = $StudentServices->del($request->all());
        if ($data === false) {
            return '操作失败';
        }
        return '操作成功';
    }

    /**
     * 查看学生信息
     *
     * @param Request            $request
     * @param StudentServices      $StudentServices
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     */
    public function info(Request $request,StudentServices  $StudentServices)
    {

        $this->validate($request, [
            'id' => 'required|integer'
        ]);
        $data = $StudentServices->info($request->all());
        if ($data === false) {
            return '操作失败';
        }
        return $data;
    }

}
